package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_against;
import cn.com.api.entity.ApiU_service;
import cn.com.api.service.ApiU_serviceService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/*
 * 帮助中心控制器
 */
@Controller
@RequestMapping(Utils.SYS_API + "helpcenter/")
public class ApiU_serviceController {

	@Autowired
	private ApiU_serviceService apiU_serviceService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_serviceController.class);

	/**
	 * 新增上门服务
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertService")
	public void insertU_service(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiU_service apiU_service) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiU_serviceService.addService(apiU_service);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 取消预约的上门服务
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "cancelService")
	public void cancelService(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String service_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiU_serviceService.cancelService(service_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 确认完成上门服务
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "confirmService")
	public void confirmService(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String service_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiU_serviceService.confirmService(service_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
